import { contextBridge } from 'electron'
import { electronAPI } from '@electron-toolkit/preload'

// Custom APIs for renderer
const api = {
  minimize: () => electronAPI.ipcRenderer.send('window-minimize'),
  maximize: () => electronAPI.ipcRenderer.send('window-maximize'),
  close: () => electronAPI.ipcRenderer.send('window-close'),
  onMaximize: (callback) => electronAPI.ipcRenderer.on('window-is-maximized', callback),
  onUnmaximize: (callback) => electronAPI.ipcRenderer.on('window-is-unmaximized', callback),
  isMaximized: () => electronAPI.ipcRenderer.invoke('window-is-maximized-sync')
}
console.log('[preload] loaded')
// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
if (process.contextIsolated) {
  try {
    contextBridge.exposeInMainWorld('electron', electronAPI)
    contextBridge.exposeInMainWorld('api', api)
  } catch (error) {
    console.error(error)
  }
} else {
  // @ts-ignore (define in dts)
  window.electron = electronAPI
  // @ts-ignore (define in dts)
  window.api = api
}
